package org.fhkoeln.sgdm.sudoku.logic;

import org.fhkoeln.sgdm.sudoku.gui.TippFrame;

public class FindBabySteps
{
	public boolean findBabySteps(SudokuField dataSource)
	{
		boolean rt = false;
		for(int i = 0; i < 9; i ++)
		{
			for(int j = 0; j < 9; j ++)
			{
				if(dataSource.values[j][i] == 0)
				{
					if(findStepForField(dataSource, i, j))
					{
						rt = true;
					}
				}
			}
		}
		return rt;
	}
	
	private boolean findStepForField(SudokuField dataSource, int x, int y)
	{
		boolean [] valuesFound = new boolean[9];
		
		//////CHECK FOR THE ROW//////
		resetBools(valuesFound);
		
		//check the row if all other fields are filled already:
		for(int i = 0; i < 9; i ++)
		{
			//do not check the field we want to make the baby check for
			if(x != i)
			{
				if(dataSource.values[y][i] != 0)
				{
					valuesFound[dataSource.values[y][i] - 1] = true;
				}
			}
		}
		//When we encounter only one false we found a baby step
		int digitMissing = 0;
		int count = 0;
		for(int i = 0; i < valuesFound.length;i ++)
		{
			if(!valuesFound[i])
			{
				digitMissing = i + 1;
				count++;
			}
		}
		if(count == 1)
		{
			//We Found a baby step!
			TippFrame.getInstance().addText("Tipp: BabyStep at line: " + (y + 1) + " value is: " + digitMissing);
			return true;
		}
		
		
		//////CHECK FOR THE COLUMN//////
		resetBools(valuesFound);
		
		//check the column if all other fields are filled already:
		for(int i = 0; i < 9; i ++)
		{
			//do not check the field we want to make the baby check for
			if(y != i)
			{
				if(dataSource.values[i][x] != 0)
				{
					valuesFound[dataSource.values[i][x] - 1] = true;
				}
			}
		}
		//When we encounter only one false we found a baby step
		digitMissing = 0;
		count = 0;
		for(int i = 0; i < valuesFound.length;i ++)
		{
			if(!valuesFound[i])
			{
				digitMissing = i + 1;
				count++;
			}
		}
		if(count == 1)
		{
			//We Found a baby step!
			TippFrame.getInstance().addText("Tipp: BabyStep at column: " + (x + 1) + " value is: " + digitMissing);
			return  true;
		}
		
		
		
		//////CHECK FOR THE QUAD//////
		resetBools(valuesFound);
		
		//check the column if all other fields are filled already:
		for(int i = 0; i < 3; i ++)
		{
			for(int j = 0; j < 3; j++)
			{
				
				//do not check the field we want to make the baby check for
				if((x%3) != i || (y%3) != j)
				{
					if(dataSource.values[j + ((y/3)*3)][i + ((x/3)*3)] != 0)
					{
						valuesFound[dataSource.values[j + ((y/3)*3)][i + ((x/3)*3)] - 1] = true;
					}
				}
			}
		}
		//When we encounter only one false we found a baby step
		count = 0;
		for(int i = 0; i < valuesFound.length;i ++)
		{
			if(!valuesFound[i])
			{
				digitMissing = i + 1;
				count++;
			}
		}
		if(count == 1)
		{
			//We Found a baby step!
			TippFrame.getInstance().addText("Tipp: BabyStep at quad; X: " + (x/3+1) + " Y: " + (y/3+1) + " Value: " + digitMissing);
			return true;
		}
		
		return false;
	}
	
	private void resetBools(boolean [] val)
	{
		for(int i = 0; i < val.length;i ++)
			val[i] = false;
	}
}
